/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import java.util.ArrayList;
import java.util.List;

import com.prefabware.commons.CollectionUtil;
import com.prefabware.commons.QualifiedName;
import com.prefabware.jmodel.code.CodeBuffer;
import com.prefabware.jmodel.code.JCodeOptions;

public class JMethodDeclaration extends JMemberDeclaration implements Cloneable{
	private JMethodSignature signature;
	public JMethodSignature getSignature() {
		if (this.signature==null) {
			this.signature=new JMethodSignature(this.getQualifiedName());
		}
		return signature;
	}

	public boolean add(JArgument e) {
		return getSignature().add(e);
	}

	public List<JArgument> getArguments() {
		return getSignature().getArguments();
	}

	public JMethodDeclaration(QualifiedName qualifiedName, JType type, JModifiers modifiers,
			JVisibility visibility, List<JType> typeArguments) {
		super(qualifiedName, type, modifiers, visibility, typeArguments);
		
	}
	public JMemberDeclaration clone(){
		return (JMemberDeclaration) super.clone();
	  }
	@Override
	public String asCode(JCodeOptions options) {
		CodeBuffer buf = new CodeBuffer(options);
		buf.setInsertLeadingSpace(false);
		buf.append(super.asCode(options));
		buf.append("(");
		List<String>argCodes=new ArrayList<String>();
		for (JArgument arg : this.getArguments()) {
			argCodes.add(arg.getDeclaration().asCode(options));
		}
		String argList = CollectionUtil.seperatedBy(argCodes, ",");
		buf.appendIfNotNullOrEmpty(argList, argList);
		buf.append(")");
		return buf.toString();
	}

}
